// pages/myset/realname/index.js
const app = getApp() // 引入app
const _request = require("../../../utils/request.js"); // 引入
require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseurl: app.globalData.baseurl, //全局baseurl路径
    imgUrl: app.globalData.imgUrl, //全局img路径
    urlimg1: 'https://kjyiyue.oss-cn-shenzhen.aliyuncs.com/images/real1.png',
    urlimg2: 'https://kjyiyue.oss-cn-shenzhen.aliyuncs.com/images/real2.png',
    urlimg3: 'https://kjyiyue.oss-cn-shenzhen.aliyuncs.com/images/real3.png',
    showtext1: false,
    showtext2: false,
    showtext3: false,
    idcode: '',
    name: '',
    userStatus: 3,
    listInfo: []
  },

  // 上传证件照
  afterRead1() {
    let _this = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: _this.data.baseurl + '/store-api/file/batchUpload',
          filePath: tempFilePaths[0],
          name: 'files',
          header: {
            'token': wx.getStorageSync('token') || null
          },
          formData: {
            'file': 'file'
          },
          success(res) {
            const data = JSON.parse(res.data).data[0]
            _this.setData({
              urlimg1: data.data,
              showtext1: true
            })
          },
          fail(error) {
            console.log(error);
          },
          complete(res) {
            console.log(res);
          },
        })
      }
    })
  },

  // 上传身份证正面
  afterRead2() {
    let _this = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: _this.data.baseurl + '/store-api/file/batchUpload',
          filePath: tempFilePaths[0],
          name: 'files',
          header: {
            'token': wx.getStorageSync('token') || null
          },
          formData: {
            'file': 'file'
          },
          success(res) {
            const data = JSON.parse(res.data).data[0]
            _this.setData({
              urlimg2: data.data,
              showtext2: true
            })
          },
          fail(error) {
            console.log(error);
          },
          complete(res) {
            console.log(res);
          },
        })
      }
    })
  },

  // 上传身份证反面
  afterRead3() {
    let _this = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: _this.data.baseurl + '/store-api/file/batchUpload',
          filePath: tempFilePaths[0],
          name: 'files',
          header: {
            'token': wx.getStorageSync('token') || null
          },
          formData: {
            'file': 'file'
          },
          success(res) {
            const data = JSON.parse(res.data).data[0]
            _this.setData({
              urlimg3: data.data,
              showtext3: true
            })
          },
          fail(error) {
            console.log(error);
          },
          complete(res) {
            console.log(res);
          },
        })
      }
    })
  },

  // 获取名字
  onChangeName(e) {
    this.setData({
      name: e.detail
    })
  },

  // 获取身份证号码
  onChangeCode(e) {
    this.setData({
      idcode: e.detail
    })
  },

  // 提交
  onSubmit() {
    if (!this.data.showtext1) {
      wx.showToast({
        title: "请上传手持证件照",
        icon: 'none',
        duration: 2000
      })
    } else if (!this.data.showtext2) {
      wx.showToast({
        title: "请上传身份证正面",
        icon: 'none',
        duration: 2000
      })
    } else if (!this.data.showtext3) {
      wx.showToast({
        title: "请上传身份证反面",
        icon: 'none',
        duration: 2000
      })
    } else if (!this.data.idcode) {
      wx.showToast({
        title: "身份证号码不能为空",
        icon: 'none',
        duration: 2000
      })

    } else if (!this.data.name) {
      wx.showToast({
        title: "姓名不能为空",
        icon: 'none',
        duration: 2000
      })
    } else {
      this.uploading()
    }
  },

  // 上传方法
  uploading() {
    _request.postObj('/userAuthApply/addOrUpdate', {
      cardFront: this.data.urlimg1,
      cardNo: this.data.idcode,
      cardReverse: this.data.urlimg3,
      hardCard: this.data.urlimg2,
      realName: this.data.name,
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      if (res.data.code == 200) {
        wx.showToast({
          title: "上传成功",
          icon: 'none',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000);
      }
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 实名状态
    _request.get('/userAuthApply/getApplyStatus', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      let status = res.data.data.applyStatus
      if (status == 1) {
        wx.setNavigationBarTitle({
          title: '已认证'
        })
        this.setData({
          bool: true
        })
        this.setData({
          name: res.data.data.realName,
          idcode: res.data.data.cardNo,
          userStatus: res.data.data.applyStatus,
          listInfo: res.data.data.listInfo
        })
      } else if (status == 0) {
        wx.setNavigationBarTitle({
          title: '审核中'
        })
        this.setData({
          name: res.data.data.realName,
          idcode: res.data.data.cardNo,
          userStatus: res.data.data.applyStatus,
          listInfo: res.data.data.listInfo
        })
        this.setData({
          bool: true
        })
      } else if (status == -1) {
        wx.setNavigationBarTitle({
          title: '审核失败'
        })
        this.setData({
          name: res.data.data.realName,
          idcode: res.data.data.cardNo,
          userStatus: res.data.data.applyStatus,
          listInfo: res.data.data.listInfo,
        })
        // 3正面身份证，4 反面身份证 ，5手持身份证
        res.data.data.listInfo.map(item => {
          if (item.type == 3) {
            this.setData({
              urlimg2: item.path,
              showtext2: true
            })
          } else if (item.type == 4) {
            this.setData({
              urlimg3: item.path,
              showtext3: true
            })
          } else if (item.type == 5) {
            this.setData({
              urlimg1: item.path,
              showtext1: true
            })
          }
        })
      } else if (status == 3) {
        wx.setNavigationBarTitle({
          title: '实名认证'
        })
      }

    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})